<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
            label-width="68px">
            <el-form-item label="答卷人id" prop="answerUserId">
                <el-input v-model="queryParams.answerUserId" placeholder="请输入答卷人id" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="最终得分" prop="score">
                <el-input v-model="queryParams.score" placeholder="请输入最终得分" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="试卷总分" prop="paperScore">
                <el-input v-model="queryParams.paperScore" placeholder="请输入试卷总分" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                    v-hasPermi="['exam:answer:add']">派发试卷</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
                    @click="handleDelete" v-hasPermi="['exam:answer:remove']">删除</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                    v-hasPermi="['exam:answer:export']">导出</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="answerList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="主键id" align="center" prop="id" />
            <el-table-column label="用户昵称" align="center" prop="answerUserName" />
            <el-table-column label="回答正确题数" align="center" prop="questionCorrect" />
            <el-table-column label="最终得分" align="center" prop="score" />
            <el-table-column label="试卷总分" align="center" prop="paperScore" />
            <el-table-column label="试卷状态" align="center" prop="status">
                <template slot-scope="scope">
                    <span v-if="scope.row.status == 1">待判分</span>
                    <span v-if="scope.row.status == 2">以判分</span>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                        v-hasPermi="['exam:answer:remove']">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize" @pagination="getList" />


        <el-dialog :title="title" :visible.sync="dispatchOpen" width="1000px">

            <el-table v-loading="userLoading" :data="userList" @selection-change="handleUserSelectionChange">
                <el-table-column type="selection" width="50" align="center" />
                <el-table-column label="用户编号" align="center" key="userId" prop="userId" />
                <el-table-column label="用户名称" align="center" key="userName" prop="userName"
                    :show-overflow-tooltip="true" />
                <el-table-column label="用户昵称" align="center" key="nickName" prop="nickName"
                    :show-overflow-tooltip="true" />
                <el-table-column label="手机号码" align="center" key="phonenumber" prop="phonenumber" width="120" />
            </el-table>

            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitAdd">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { listAnswer, getAnswer, delAnswer, addAnswer, updateAnswer, unDispatchStuList, addAnswerBatch } from "@/api/exam/answer";

export default {
    name: "Answer",
    data() {
        return {
            examPaperId: null,
            // 遮罩层
            loading: true,
            userLoading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 学生试卷答案表格数据
            answerList: [],
            userList: [],
            addUserList: [],
            deleteUserList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false, dispatchOpen: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                examPaperId: null,
                answerUserId: null,
                questionCorrect: null,
                score: null,
                paperScore: null,
                status: null,
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                examPaperId: [
                    { required: true, message: "回答的试卷id不能为空", trigger: "blur" }
                ],
                answerUserId: [
                    { required: true, message: "答卷的用户id不能为空", trigger: "blur" }
                ],
                questionCorrect: [
                    { required: true, message: "回答正确题数不能为空", trigger: "blur" }
                ],
                score: [
                    { required: true, message: "最终得分不能为空", trigger: "blur" }
                ],
                paperScore: [
                    { required: true, message: "试卷总分不能为空", trigger: "blur" }
                ],
                status: [
                    { required: true, message: "试卷状态1待判分，2完成不能为空", trigger: "blur" }
                ],
                createTime: [
                    { required: true, message: "试卷提交时间不能为空", trigger: "blur" }
                ]
            }
        };
    },
    created() {
        this.examPaperId = this.$route.params && this.$route.params.examPaperId;
        if (this.examPaperId) {
            this.queryParams.examPaperId = this.examPaperId
            this.form.examPaperId = this.examPaperId
            this.getList();
        }
    },
    methods: {
        /** 查询该场考试的学生 */
        getList() {
            this.loading = true;
            listAnswer(this.queryParams).then(response => {
                console.log(response)
                this.answerList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.dispatchOpen = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                examPaperId: this.examPaperId,
                answerUserId: null,
                questionCorrect: null,
                score: null,
                paperScore: null,
                status: 0,
                createTime: null
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            let ids = selection.map(item => item.id)
            this.ids = ids;
            this.deleteUserList = [];
            for (const id of ids) {
                this.deleteUserList.push({ examPaperId: this.examPaperId, answerUserId: id })
            }
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        handleUserSelectionChange(selection) {
            let ids = selection.map(item => item.userId)
            this.addUserList = [];
            for (const id of ids) {
                this.addUserList.push({ examPaperId: this.examPaperId, answerUserId: id })
            }
        },
        /** 派发试卷 */
        handleAdd() {
            this.reset();
            this.dispatchOpen = true;
            this.title = "派发试卷";
            this.userLoading = true;
            unDispatchStuList(this.examPaperId).then(res => {
                this.userList = res.data;
                this.userLoading = false;
            })
        },
        // 提交派发试卷
        submitAdd() {
            addAnswerBatch(this.addUserList).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.dispatchOpen = false;
                this.getList();
            });
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const id = row.id || this.ids
            getAnswer(id).then(response => {
                this.form = response.data;
                this.open = true;
                this.title = "修改学生试卷答案";
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$modal.confirm('是否确认删除学生').then(function () {
                return delAnswer(ids);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => { });
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download('exam/answer/export', {
                ...this.queryParams
            }, `answer_${new Date().getTime()}.xlsx`)
        }
    }
};
</script>
